window.domain = 'http://www.yoomeda.com:1984';
// window.domain = 'http://www.yoomeda.com';

window.requestUrl = {
    registerUrl: '/api/v1/sys/user/',
    loginUrl: '/api/v1/sys/session/user/',
    logoutUrl: '/api/v1/sys/session/user',
    registerCodeUrl: '/api/v1/code/register',
    forgetCodeUrl: '/api/v1/code/forget_pass',
    imgCodeUrl: '/api/v1/img/verification',
    resetPassUrl: '/api/v1/sys/user/password',
    userUrl: '/api/v1/sys/session/user',
    updateUserUrl: '/api/v1/sys/session/user',
    checkPlatUserUrl: '/api/v2/host/check/',
    addPlatInfoUrl: '/api/v2/host/plat/add/',
    getPlatInfoUrl: '/api/v2/plat/all',
    singleInfoUrl: '/api/v2/plat/detail',
    updatePlatInfoUrl: '/api/v2/plat/plat',
    delPlatInfoUrl: '/api/v2/plat/plat',
    getPlatTagUrl: '/api/v2/public/plat/tag/',
    getEditInfoUrl: '/api/v1/extra/intro/',
    delInfoUrl: '/api/v1/extra/intro/del/',
    getDetailUrl: '/api/v2/public/platInfo',
    relatePlatsUrl: '/api/v2/public/relatePlats',
    relatedPagesUrl: '/api/v2/public/relatedPages',
    getCompanyUrl: '/api/v1/extra/company',
    getCompanyPlatUrl: '/api/v1/extra/plats',
    setCompanyUrl: '/api/v1/extra/company',
    getIndexRecomUrl: '/api/v1/extra/index',
    searchUrl: '/api/v1/extra/search/',
    filtrateTagsUrl: '/api/v2/public/filtrateTags',
    selectSearchUrl: '/api/v2/public/search',
    weixinloginUrl: '/api/v2/host/url/4',
    checkStatusweinUrl: '/api/v2/host/checkStatus',
    addWeixinUrl: '/api/v2/host/plat/add/4',
    sinaloginUrl: '/api/v2/host/url/5',
    checkStatuswbUrl: '/api/v2/host/checkStatus',
	addWbUrl: '/api/v2/host/plat/add/5'
}

window.getUrlParam = function(name, url) {
    var re = new RegExp("[\\?&#]" + name + "=([^&#]+)","gi");
    var ma = (url || location.href).match(re);
    var strArr;

    if (ma && ma.length > 0) {
        strArr = (ma[ma.length-1]).split("=");
        if (strArr && strArr.length > 1) {
            return strArr[1];
        }
        return ''
    }
    return '';
}

window.setCookie = function(name, value) {
    var Days = 30;
     var exp = new Date();
     exp.setTime(exp.getTime() + Days*24*60*60*1000);
     document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
},
window.getCookie = function(name) {
    var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
     if(arr=document.cookie.match(reg))
     return unescape(arr[2]);
     else
     return null;
},
window.delCookie = function(name) {
    var exp = new Date();
     exp.setTime(exp.getTime() - 1);
     var cval=window.getCookie(name);
     if(cval!=null)
     document.cookie= name + "="+cval+";expires="+exp.toGMTString();
}

window.formatTime = function(time) {
    var date = new Date(time);
    var month = (date.getMonth() + 1) < 10 ? '0'+(date.getMonth() + 1): (date.getMonth() + 1);
    var day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
    return date.getFullYear() + '-' +  month + '-' + day;
}

var common = {
    logout: function() {
        $('[data-role="logout"]').on('click', function() {
            $.ajax({
                url: window.domain + window.requestUrl.logoutUrl,
                type: 'delete',
                xhrFields: {
                    withCredentials: true
                },
                crossDomain: true,
                success: function(res) {
                    if(typeof(res.not_login) != 'undefined') {
                        window.setCookie('go_back_link', window.location.href);
                        window.location.href = './login.html';
                        return;
                    }
                    if(res.status) {
                        iosOverlay({
                            text: '退出成功',
                            duration: 1000,
                            onbeforehide: function() {
                                window.location.href = './index.html';
                            }
                        });
                    } else {
                        iosOverlay({
                            text: res.msg,
                            duration: 1000
                        });
                    }
                },
                error: function(e) {
                }
            })
        })
    },
    search: function() {
        $('[data-role="search"]').on('click', function() {
            var keyword = $(this).prev('input').val();
            var href = window.location.href.split('?')[0];
            window.location.href = href + '?keyword=' + keyword;
        })
        $('[data-role="search"]').prev('input').on('keypress', function(e) {
            if(e.keyCode == 13) {
                var keyword = $(this).val();
                var href = window.location.href.split('?')[0];
                window.location.href = href + '?keyword=' + keyword;
            }
        })
    },
    loginStatus: function() {
        $.ajax({
            url: window.domain + window.requestUrl.userUrl,
            type: 'get',
            xhrFields: {
                withCredentials: true
            },
            crossDomain: true,
            success: function(res) {
                if(typeof(res.not_login) != 'undefined') {
                    window.setCookie('go_back_link', window.location.href);
                    window.location.href = './login.html';
                    return;
                }
                if(res.status) {
                    $('.header-controls').hide();
                    $('.header-logined').show();
                    if(res.data.user.type == 1) {
                        $('.header-logined__cover').prepend('<li><a href="./weixin-show.html">管理中心</a></li>');
                    }
                }
            },
            error: function(e) {
            }
        })
    },
    delItem: function() {
        $(document).on('click', '[data-role="del"]', function() {
            var $this = $(this);
            $.ajax({
                url: window.domain + window.requestUrl.delPlatInfoUrl + '?id=' + $this.attr('data-id'),
                type: 'delete',
                xhrFields: {
                    withCredentials: true
                },
                crossDomain: true,
                success: function(res) {
                    if(typeof(res.not_login) != 'undefined') {
                        window.setCookie('go_back_link', window.location.href);
                        window.location.href = './login.html';
                        return;
                    }
                    if(res.status) {
                        $this.closest('tr').remove();
                    } else {
                        iosOverlay({
                            text: res.msg,
                            duration: 1000
                        });
                    }
                },
                error: function(e) {
                }
            })
        })
    },
    setSearch: function() {
        $('[data-role="search"]').prev('input').val(decodeURI(window.getUrlParam('keyword')));
    },
    // select页面选中
    activeSelectPage: function() {
        var href = window.location.href;
        var map = ['index.html', 'weixin-select.html', 'sina-select.html', 'wanghong-select.html', 'video-select.html', 'headline-select.html'];
        $.each(map, function(index, item) {
            if(href.indexOf(item) != -1) {
                $('.header-nav li').eq(index).addClass('is-active');
            }
        })

        var searchmap = ['weixin-select.html', 'sina-select.html', 'wanghong-select.html', 'video-select.html', 'headline-select.html'];
        $.each(searchmap, function(index, item) {
            if(href.indexOf(item) != -1) {
                $('.header-search').show();
            }
        })
    },
    // 返回顶部
    toTop: function() {
        $('[data-role="to-top"]').on('click', function() {
             $('body, html').animate({
                 scrollTop: 0
             }, 700);
        })

        // 显示qq && 电话
        $('[data-role="showPop"]').on('mouseenter', function() {
            $(this).find('.window-item__show').stop().fadeIn('fast');
        }).on('mouseleave', function() {
            $(this).find('.window-item__show').stop().fadeOut('fast');
        })
    },
    init: function() {
        this.logout();
        this.search();
        this.loginStatus();
        this.delItem();
        this.setSearch();
        this.activeSelectPage();
        this.toTop();
    }
}

common.init();
